//package cn.fl.login.controller.filter;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import cmm.user.facade.system.api.ISysResPowerApiService;
//import cn.cmm.base.framework.exception.BaseException;
//import cn.fl.login.loginEnum.LoginType;
//import cn.fl.login.token.EasyTypeToken;
//import cmm.user.facade.system.api.ISysUsrQueryApiService;
//import cn.fl.system.service.ISysUsrService;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.Subject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import cmm.user.facade.system.dto.SysUsrDTO;
//import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
//import cn.cmm.base.framework.constant.BaseConstant;
//import cn.cmm.base.framework.utils.CollectionUtils;
//import cn.fl.framework.base.context.CurrentThreadContext;
//
///**
// * SHIRO 认证
// * @author FUJINGRUN
// *
// */
//public class ShiroRealm extends AuthorizingRealm {
//
//	private static final Logger logger = LoggerFactory.getLogger(ShiroRealm.class);
//
//
//	@Autowired
//	private ISysResPowerApiService sysResPowerApiService;
//
//	@Autowired
//	private ISysUsrService sysUsrService;
//
//	@Override
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
//		return null;
//	}
//
//	@Override
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
//		SysUsrDTO sysUsrDTO =null;
//		EasyTypeToken token=(EasyTypeToken)authcToken;
//		String usrNm = token.getUsername();
//		String type=token.getType().getCode();
//		String pwd = new String(token.getPassword());
//
//		//用户使用密码登录系统
//		if(LoginType.PASSWORD.getCode().equals(type)){
//			sysUsrDTO=this.sysUsrService.checkSysUsrPwd(usrNm,pwd);
//		}else if(LoginType.SCANCODE.getCode().equals(type)) {
//			pwd="111111";
//			//扫码登录
//			sysUsrDTO = this.sysUsrService.selectSysUsrWithAuthBy(CurrentThreadContext.getCurrentSysCd(), token.getUsername());
//		}else{
//			throw new BaseException("系统暂不支持该登录方式登录，请使用其它方式登录");
//		}
//
//		//HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
//		//获取用户
//		if(null != sysUsrDTO){
//			Subject subject = SecurityUtils.getSubject();
//			Session session = subject.getSession(true);
//			session.setTimeout(BaseConstant.SESSION_MAX_INACTIVE_INTERVAL);
//			//session.setMaxInactiveInterval(BaseConstant.SESSION_MAX_INACTIVE_INTERVAL);
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_ID, sysUsrDTO.getId());
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_NAME, sysUsrDTO.getRefRlNm());
//			CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_USER_INFO, sysUsrDTO);
//			session.setAttribute(CurrentThreadContext.CURRENT_USER_ID, sysUsrDTO.getId());
//			session.setAttribute(CurrentThreadContext.CURRENT_USER_NAME, sysUsrDTO.getRefRlNm());
//			session.setAttribute(CurrentThreadContext.CURRENT_USER_INFO, sysUsrDTO);
//			List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = sysUsrDTO.getSysUsrFunAuthRDTOList();
//			//获取默认的组织架构和角色
//			if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
//				SysUsrFunAuthRDTO sysUsrFunAuthRDTO = null;
//				for (SysUsrFunAuthRDTO sfa : sysUsrFunAuthRDTOList) {
//					if(Integer.valueOf(BaseConstant.IS_YESNO_YES).equals(sfa.getRoleIsVis())){
//						sysUsrFunAuthRDTO = sfa;
//    					break;
//    				}
//				}
//				//如果角色都是不显示的则返回null 属于登录失败
//				if(null == sysUsrFunAuthRDTO){
//					return null;
//				}
//
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, sysUsrFunAuthRDTO.getOrgId());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, sysUsrFunAuthRDTO.getOrgShtNm());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, sysUsrFunAuthRDTO.getOrgDispNm());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_TYP_CD, sysUsrFunAuthRDTO.getOrgTypCd());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());
//
//
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_ID, sysUsrFunAuthRDTO.getOrgId());
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_SHT_NM, sysUsrFunAuthRDTO.getOrgShtNm());
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_DISP_NM, sysUsrFunAuthRDTO.getOrgDispNm());
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_TYP_CD, sysUsrFunAuthRDTO.getOrgTypCd());
//                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
//                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());
//
//				Boolean rtn=this.sysUsrService.getResPowerCtrl();
//				if(rtn) {
//					List<String> roleUrls = this.sysResPowerApiService.getSysResPowereListByRoleIds(sysUsrFunAuthRDTO.getRoleId(), CurrentThreadContext.getCurrentSysCd());
//					CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, roleUrls);
//					session.setAttribute(CurrentThreadContext.CURRENT_RES_POWER_URL, roleUrls);
//				}
//
//                List<Long> roleIds = new ArrayList<>();
//				//要把所有角色赋值给线程变量,给前端使用
//				for (SysUsrFunAuthRDTO sufard : sysUsrFunAuthRDTOList) {
//					roleIds.add(sufard.getRoleId());
//				}
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, roleIds);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_GRP_ID, roleIds);
//			}else{
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_TYP_CD, null);
//                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD, null);
//                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, null);
//                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_GRP_ID, null);
//				CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, null);
//
//                session.setAttribute(CurrentThreadContext.CURRENT_ORG_CD, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_ID, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_ID, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_SHT_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_DISP_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ROLE_GRP_ID, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_ORG_TYP_CD, null);
//                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD, null);
//                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, null);
//				session.setAttribute(CurrentThreadContext.CURRENT_RES_POWER_URL, null);
//            }
//			//pass = true;
//			session.setAttribute(CurrentThreadContext.CURRENT_CHANGE_ROLE_FLAG, false);
//			return new SimpleAuthenticationInfo(sysUsrDTO.getUsrNm(), pwd, sysUsrDTO.getRefRlNm());
//		}
//		return null;
//	}
//
//
//
//}
